package im.composer.media.audio.stream;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

public class LinkedByteArrayInputStream extends InputStream {

	private final BlockingQueue<byte[]> queue = new LinkedBlockingQueue<byte[]>();
	private ByteArrayInputStream current = new ByteArrayInputStream(new byte[] {});

	private void checkNext() {
		if (current.available() < 1 && !queue.isEmpty()) {
			current = new ByteArrayInputStream(queue.poll());
		}
	}

	@Override
	public int available() throws IOException {
		checkNext();
		return current.available();
	}

	@Override
	public int read() throws IOException {
		checkNext();
		return current.read();
	}

	public boolean offer(byte[] e) {
		return queue.offer(e);
	}

}
